// pages/andon_kanban/xqjyzz/xqjyzz.js
const mybehavior = require("../../../behaviors/requests")
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    page: 1,
    total: 0,
    page_all: 0,
    base_url: "https://logisticsengineering.gree.com/andon_system",
    location: "二车间汇总",
    current: 1,
    list: [],
    option1: [
      { text: '二车间汇总', value: '二车间汇总' },
    ],
    value1: '二车间汇总',
    show: false,
    detail_list: [],
    steps: [
      {
        text: '创建中',
        desc: '',
      },
      {
        text: '正在上料',
        desc: '',

      },
      {
        text: '前往取料',

      },
      {
        text: '正在拉料',
        desc: '',

      },
      {
        text: '确认收料',
        desc: '',

      },
      {
        text: '已完成',
        desc: '',

      },
    ],
    title:"二车间汇总",
    title1: "",
    title2: "",
    title3: "",
    title4: "上一次",
    active: 0

  },
  pageUp() {
    let now_page = this.data.page
    let val = this.data.title
    if (now_page != 1) {
      now_page = now_page - 1
      this.setData({
        page: now_page,
      })
      this.getDatas(val)
    }


  },
  pageDown() {
    let now_page = this.data.page
    let val = this.data.title
    if (now_page < this.data.page_all) {
      now_page = now_page + 1
      this.setData({
        page: now_page
      })
      this.getDatas(val)
    }

  },
  // 切换上次、本次详情
  detailSelect() {
    let t = this.data.title4
    let data_arr = this.data.detail_list
    let step_arr = this.data.steps
    if (t == "上一次") {
      this.setData({
        title4: "回到本次",
        title3: data_arr[1].all_times
      })
      step_arr[0].desc = data_arr[1].start_time
      step_arr[1].desc = data_arr[1].state_time1
      step_arr[2].desc = data_arr[1].state_time2
      step_arr[3].desc = data_arr[1].state_time3
      step_arr[4].desc = data_arr[1].state_time4
      step_arr[5].desc = data_arr[1].over_time
      this.setData({
        steps: step_arr,
        active: 5
      })
    } else {
      this.setData({
        title3: data_arr[0].all_times,
        title4: "上一次"
      })
      step_arr[0].desc = data_arr[0].start_time
      step_arr[1].desc = data_arr[0].state_time1
      step_arr[2].desc = data_arr[0].state_time2
      step_arr[3].desc = data_arr[0].state_time3
      step_arr[4].desc = data_arr[0].state_time4
      step_arr[5].desc = data_arr[0].over_time
      this.setData({
        active: data_arr[0].step,
        steps: step_arr
      })
    }
  },
  // 详情弹出框
  onCaoZuo(e) {
    let row = e.currentTarget.dataset.row_data
    console.log(row);
    this.setData({
      show: true,
      title1: row.location,
      title2: row.material_name,
      title3: row.all_times,
    })
    let that = this
    let url = this.data.base_url + "/ZhongS_progress_bar"
    let data = {
      location: this.data.value1,
      material_location: row.location,
      material_name: row.material_name,
      current: 1,
    }
    wx.request({
      url,
      data,
      dataType: "json",
      method: "GET",
      success(res) {
        const result = res.data;
        const code = result.code;
        if (code == 20000) {
          // console.log("11", result);
          that.setData({
            detail_list: result.pop_data
          })
          console.log(that.data.detail_list);
          let step_arr = that.data.steps
          step_arr[0].desc = that.data.detail_list[0].start_time
          step_arr[1].desc = that.data.detail_list[0].state_time1
          step_arr[2].desc = that.data.detail_list[0].state_time2
          step_arr[3].desc = that.data.detail_list[0].state_time3
          step_arr[4].desc = that.data.detail_list[0].state_time4
          step_arr[5].desc = that.data.detail_list[0].over_time
          that.setData({
            steps: step_arr,
            active: that.data.detail_list[0].step
          })

        } else {
          wx.showToast({
            title: result.msg,
            icon: 'error'
          })
        }
      },
      fail(res) {
        console.log(res);
        wx.showToast({
          title: '服务器出现错误！',
          icon: 'error'
        })

      }
    });
  },

  onClose() {
    this.setData({
      show: false,
      title1: "",
      title2: "",
      title3: "",
      title4: "上一次",
      active: 0
    })
  },
  // 下拉筛选框
  onDropdownChange(e) {
    console.log(e.detail);
    this.getDatas(e.detail)
    this.setData({
      page:1,
      title:e.detail,
    })
  },
  // 获取数据
  getDatas(ev) {
    let that = this
    let url = this.data.base_url + "/ZhongS_change_table"
    let data = {
      location: ev,
      current: this.data.page,
    }
    wx.request({
      url,
      data,
      dataType: "json",
      method: "GET",
      success(res) {
        const result = res.data;
        const code = result.code;
        if (code == 20000) {
          // console.log("11", result);
          that.setData({
            list: result.table_data,
            total: result.table_total,
            page_all: Math.ceil(result.table_total / 10)
          })

        } else {
          wx.showToast({
            title: result.msg,
            icon: 'error'
          })
        }
      },
      fail(res) {
        console.log(res);
        wx.showToast({
          title: '服务器出现错误！',
          icon: 'error'
        })

      }
    });
  },
  // 获取西区线体数据
  getLineDatas() {
    let that = this
    let url = this.data.base_url + "/get_ZhongS_line_list"
    let data = {

    }
    wx.request({
      url,
      data,
      dataType: "json",
      method: "GET",
      success(res) {
        const result = res.data;
        const code = result.code;
        if (code == 20000) {
          console.log("11", result);
          let r = result.data
          let options = that.data.option1
          for (let i in r) {
            let val_arr = {
              text: '', value: ''
            }
            val_arr.text = r[i]
            val_arr.value = r[i]
            options.push(val_arr)
          }
          that.setData({
            option1: options
          })

        } else {
          wx.showToast({
            title: result.msg,
            icon: 'error'
          })
        }
      },
      fail(res) {
        console.log(res);
        wx.showToast({
          title: '服务器出现错误！',
          icon: 'error'
        })

      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let ev = "二车间汇总"
    this.getDatas(ev)
    this.getLineDatas()
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})